﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Slam.Configuration;
using Slam;
using Slam.SharePoint;
using System.Data.Linq;
using SlamCms.SharePoint.Tagging.Data;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;
using SlamCms.SharePoint.Data;
using System.Data.EntityClient;

namespace SlamCms.SharePoint.Tagging.Services
{
	public class FeatureOnTagFieldServices : TaggingServices
	{
		private static Regex valueFormat = new Regex("(?<=##)(.*?)(?=##)", RegexOptions.Compiled);
		
		public FeatureOnTagFieldServices() : 
			base()
		{	
		}

		public static List<FeatureOnTagValue> ParseFeatureOnTagValue(string value)
		{
			List<FeatureOnTagValue> values = new List<FeatureOnTagValue>();

			foreach (Match valueMatch in valueFormat.Matches(HttpUtility.HtmlDecode(value)))
			{
				string[] parts = valueMatch.Value.Split(';');
				FeatureOnTagValue tagValue = new FeatureOnTagValue();
				tagValue.Id = parts[0];
				if (!String.IsNullOrEmpty(tagValue.Id) && tagValue.Id != "0")
				{
					tagValue.Title = parts[1];
					tagValue.FeatureLevel = parts[2];
					tagValue.Type = (FeatureType)Enum.Parse(typeof(FeatureType), parts[3]);

					values.Add(tagValue);
				}
			}

			return values;
		}

		public static string GetFeatureOnTagValue(List<FeatureOnTagValue> values)
		{
			string fieldValue = "##";
			foreach (FeatureOnTagValue value in values)
			{
				fieldValue += String.Format("{0};{1};{2};{3}##", value.Id, value.Title, value.FeatureLevel, value.Type.ToString());
			}
			return fieldValue;
		}
	}
}
